package cn.droidlover.xdroidmvp.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import cn.droidlover.xdroidmvp.imageloader.ILFactory;
import cn.droidlover.xdroidmvp.imageloader.ILoader;


/**
 * Created by Administrator on 2017/4/6.
 */

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;//集合类，layout里包含的View,以view的id作为key，value是view对象
    private Context mContext;//上下文对象

    public RecyclerViewHolder(Context ctx, View itemView) {
        super(itemView);
        mContext = ctx;
        mViews = new SparseArray<View>();
    }


    protected View getItemView() {
        return itemView;
    }

    private <T extends View> T findViewById(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            if (null != view) {
                mViews.put(viewId, view);
            } else {
                return null;
            }
        }
        return (T) view;
    }

    public <T extends View> T getView(int viewId) {
        return findViewById(viewId);
    }

    public TextView getTextView(int viewId) {
        return (TextView) getView(viewId);
    }

    public Button getButton(int viewId) {
        return (Button) getView(viewId);
    }

    public ImageView getImageView(int viewId) {
        return (ImageView) getView(viewId);
    }

    public ImageButton getImageButton(int viewId) {
        return (ImageButton) getView(viewId);
    }

    public EditText getEditText(int viewId) {
        return (EditText) getView(viewId);
    }

    public RecyclerViewHolder setText(int viewId, String value) {
        if (TextUtils.isEmpty(value)) {
            value = "";
        }
        if (value.contains("null")) {
            value = value.replace("null", "");
        }
        TextView view = findViewById(viewId);
        if (view == null) {
            return this;
        }
        view.setText(value);
        view.setVisibility(View.VISIBLE);
        return this;
    }

    public RecyclerViewHolder setBackground(int viewId, int resId) {
        View view = findViewById(viewId);
        if (null != view) {
            view.setBackgroundResource(resId);
        }
        return this;
    }

    public void setImageResDrawable(int viewId, int resId) {
        ImageView view = getImageView(viewId);
        view.setVisibility(View.VISIBLE);
        view.setImageResource(resId);
    }

    public RecyclerViewHolder setClickListener(int viewId, View.OnClickListener listener) {
        View view = findViewById(viewId);
        if (null == view) {
            return this;
        }
        view.setOnClickListener(listener);
        return this;
    }

    public void setVisibility(int viewId, int visibility) {
        View view = findViewById(viewId);
        if (view != null) {
            view.setVisibility(visibility);
        }
    }

    public void setImagePic(int viewId, String url) {
        ILFactory.getLoader().loadNet(getImageView(viewId),url,ILoader.Options.defaultOptions());
    }
    public void setImagePic(int viewId, String url, ILoader.Options options) {
        ILFactory.getLoader().loadNet(getImageView(viewId),url,options);
    }

    public void setImagePicPath(int viewId, String path, ILoader.Options options) {
        ILFactory.getLoader().loadFile(getImageView(viewId),new File(path),options);
    }

    public void setChildOnClick(final int position,int viewId,final ListViewChildClickListener listener){
        View view = getView(viewId);
        if (null != view && listener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.itemChildClick(position,v.getId());
                }
            });
        }

    }
}